#!/bin/bash

#切换到脚本所在目录并保存绝对路径
cd $(dirname "$0")
SELFDIR=`pwd`

INSTALLDIR=/usr/local/mno
LINK_NAMES="node npm npx"
LINK_DIR='/usr/local/bin'
SU_LINK_DIR="$LINK_DIR"
LINK_DIR2='/usr/bin'
#当前用户主目录下若存在bin，切换版本的时候，可以覆盖默认的链接。
USER_DIR="$HOME/bin"

TMP_DNAME=''
TMP_VNAME=''

TMP_VERSION=''

SUDO=sudo

if [ "$UID" -ne 0 ] ; then
    LINK_DIR="$USER_DIR"
    if [ ! -d "$USER_DIR" ] ; then
        mkdir $USER_DIR
    fi
else
    SUDO=''
fi

parse_version () {
    TMP_VERSION=`echo $1 | awk -F '-' '{printf $2}'`
}

NDLIST=''

SYS_ARCH='x64'

. "$INSTALLDIR/arch.sh"

get_version_list () {
    NDLIST_CACHE=`ls $INSTALLDIR | sort -r`
    for a in $NDLIST_CACHE ; do
        if echo $a | grep "node-v.*-linux-$SYS_ARCH" > /dev/null ; then
            NDLIST="$NDLIST $a"
        fi
    done
}

VNLIST=''
get_vn_list () {
    get_version_list

    for a in $NDLIST ; do
        parse_version $a
        VNLIST="$VNLIST $TMP_VERSION"
    done
}

CUR_VERSION=''
get_cur_version () {
    #CUR_VERSION=`cat $INSTALLDIR/cur_version`
    if [ "$UID" -eq 0 ] ; then
        CUR_VERSION=`$LINK_DIR/node -v`
    else
        CUR_VERSION=`node -v`
    fi
}

out_version_list () {
    get_version_list
    get_cur_version
    
    echo "已安装版本(Installed)："

    for a in $NDLIST ; do
        parse_version $a
        if [ "$TMP_VERSION" = "$CUR_VERSION" ] ; then
            echo "    $TMP_VERSION *"
        else
            echo "    $TMP_VERSION"
        fi
    done
}

VNAME=''
fmt_version () {
    VNAME=$1
    
    if [ "${VNAME:0:1}" != "v" ] ; then
        VNAME="v$VNAME"
    fi
}

make_dname () {
    fmt_version $1
    TMP_DNAME="node-$VNAME-linux-$SYS_ARCH"
}

set_version () {
    get_cur_version
    
    if [ "$CUR_VERSION" = "$1" ] ; then
        return 0
    fi

    make_dname $1
    for n in $LINK_NAMES ; do
        if [ -L "$LINK_DIR/$n" ] ; then
            rm $LINK_DIR/$n
        fi
        ln -s $INSTALLDIR/$TMP_DNAME/bin/$n $LINK_DIR/$n

        if [ "$UID" -eq 0 ] ; then
            if [ -L "$LINK_DIR2/$n" ] ; then
                rm $LINK_DIR2/$n
            fi

            ln -s $INSTALLDIR/$TMP_DNAME/bin/$n $LINK_DIR2/$n
        fi
    done

    echo "set to $1 OK"
}

remove_version () {
    make_dname $1
    fmt_version $1

    if [ ! -d "$INSTALLDIR/$TMP_DNAME" ] ; then
        echo "没有此版本，不需要移除。"
        return 1
    fi
    
    get_cur_version

    if [ "$CUR_VERSION" = "$VNAME" ] ; then
        echo "当前版本正在使用，不能删除($CUR_VERSION in used)。"
        return 1
    fi

    if [ "$UID" -ne 0 ] ; then
        sudo rm -rf "$INSTALLDIR/$TMP_DNAME"
    else
        rm -rf "$INSTALLDIR/$TMP_DNAME"
    fi

    if [ "$?" -ne 0 ] ; then
        echo "remove $1 failed"
        return 1
    fi

    echo "remove $1 ok"

    return 0
}

get_help () {
    echo "mno node.js版本管理工具"
    echo "    使用方式：mno [CMD] [OPTIONS]"
    echo "    命令 ： mno [change|list|version|v|reset|remove|install]"
    echo "    change  : 切换版本"
    echo "    list    : 显示已安装版本"
    echo "    version : 当前版本"
    echo "    v       : version的别名"
    #echo "    reset   : 重置为最新版本"
    echo "    remove  : 删除版本"
    echo "    install : 安装指定版本的Node.js"
    echo "    clear   : 清除已经下载的安装包"
    echo "    autoclear : 自动清理，只保留每个主版本号最新，"
    echo "            并自动设置使用最新版，自动清除已经下载的安装包。"
    echo "    update  : 更新Node.js版本"
    echo "    u       : update的别名"
    echo "    切换版本示例：mno change v16.19.3"
    echo "    删除版本示例：mno remove v10.22.1"
    echo "    删除多个版本示例：mno remove 'v12.16.1 v14.16.1'"
    echo "    安装示例：mno install v12.22.5"
}

ORGURL=''

if [[ "$#" -gt 1 ]] && [[ "$2" = "--org" ]] ; then
    ORGURL=$2
fi

ARGS=''
ARGS_COUNT=0

for a in $@ ; do
    if [ $ARGS_COUNT -gt 0 ] ; then
        ARGS="$ARGS $a"
    fi

    let ARGS_COUNT+=1
done

case "$1" in
    # change version
    change)
        make_dname $2
        if [ ! -d "$INSTALLDIR/$TMP_DNAME" ] ; then
            echo "未知版本"
            exit 1
        fi
        set_version $2
        ;;

    list)
        get_vn_list
        echo $VNLIST
        ;;

    version)
        get_cur_version
        echo $CUR_VERSION
        ;;
    v)
        get_cur_version
        echo $CUR_VERSION
        ;;

    remove)
        for a in $ARGS ; do
            remove_version $a
        done
        ;;

#    reset)
#        get_vn_list
#        for a in $VNLIST ; do
#            set_version $a
#            break
#        done
#        ;;

    clear)
        if [ -d "$INSTALLDIR/versions" ] ; then
            PKGLIST=`ls $INSTALLDIR/versions`

            if [ -n "$PKGLIST" ] ; then
                sudo rm $INSTALLDIR/versions/*
            fi
        fi

        if [ -d "$INSTALLDIR/shasum" ] ; then
            SHASUM_LIST=`ls $INSTALLDIR/shasum`
            get_vn_list
            for a in $SHASUM_LIST ; do
                if echo $VNLIST | grep $a > /dev/null ; then
                    continue
                fi
                sudo rm "$INSTALLDIR/shasum/$a"
            done
        fi
        ;;

    autoclear)
        DEL_LIST=`$SUDO node $INSTALLDIR/autoclear.js`
        MAIN_COUNT=0
        for d in $DEL_LIST ; do
            if [ "$MAIN_COUNT" -eq 0 ] ; then
                MAIN_COUNT=1
                if [ "$UID" -ne 0 ] ; then
                    sudo mno change $d
                fi
                mno change $d
            else
                $SUDO mno remove $d
            fi
        done

        $SUDO mno clear
        ;;
    install)
        sudo $INSTALLDIR/install.sh -v $ARGS --no-default
        ;;

    update)
        sudo $INSTALLDIR/install.sh --no-default $ORGURL
        ;;

    u)
        sudo $INSTALLDIR/install.sh --no-default $ORGURL
        ;;
    *)
        out_version_list
        get_help
        ;;
esac
